<?php

namespace App\Http\Controllers\YApi;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use Redirect;

use App\Models\Wxusers;

class WxusersController extends Controller
{

  	public function wxusers()
  	{
		// 记录微信注册用户信息
		$wxuserinfo = request::input('wxusers'); // 用户信息
		$openid = request::input("openid");
		$unionid = request::input("unionid");
		$session_key = request::input("session_key");		

		// return $wxuserinfo['city'];

		$wxusers = Wxusers::where('openid', $openid)->first();
		if($wxusers == null) 
		{
			// 如果没有记录过微信会员信息，那么添加
			$wxusers = new Wxusers;

			$wxusers->openid = $openid;
			$wxusers->unionid = $unionid;
			$wxusers->session_key = $session_key;

			$wxusers->nickName = $wxuserinfo['nickName'];
			$wxusers->gender = $wxuserinfo['gender'];
			$wxusers->language = $wxuserinfo['language'];
			$wxusers->city = $wxuserinfo['city'];
			$wxusers->province = $wxuserinfo['province'];
			$wxusers->country = $wxuserinfo['country'];
			$wxusers->avatarUrl = $wxuserinfo['avatarUrl'];

			$wxusers->add_time = time();
			$wxusers->save();

			return 1; // 添加数据
		}

		return 2; // 数据已经存在，不再添加数据
		
    }
    
}